const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
//页面跳转后，如果页面不刷新，通过pageOnload强制刷新 
const pageOnLoad = () => {
  const currentPage = getCurrentPages().pop();
  if (currentPage == null || currentPage == undefined) return
  currentPage.onLoad();
}

//判断当前用户是否已授权登录 
const cklogin = (userInfo) => {
  return new Promise((resolve, reject) => {
    if (!userInfo) {
      wx.showModal({
        title: '提示',
        content: '请先授权登录',
        showCancel: false
      }).then(res => {
        if (res.confirm) {
          wx.switchTab({
            url: '../person/person',
          })
        }
      })
      resolve(false)
    } else {
      resolve(true)
    }
  })
}
//表单验证错误提示 
const showValidateModal = error => {
  wx.showModal({
    content: error.msg,
    showCancel: false,
  })
}

module.exports = {
  formatTime,
  pageOnLoad,
  cklogin,
  showValidateModal
}